package ce.com.newbluedemo.k2;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import ce.com.cenewbluesdk.entity.k2.K2_Action;
import ce.com.cenewbluesdk.entity.k2.K2_DevInfoStruct;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;

/**
 * Created by YaoBinkun on 2016/12/29.
 */

public class ActivityGetDevInfo extends K2BaseActivity {
    private K2_DevInfoStruct mK2_devInfoStruct;
    private TextView mTvCustomerId, mTvHardwareId, mTvCodeId;


    private CEDevK2Proxy mK2Proxy;
    private Handler mK2Handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == K2_Action.RCVD.RCVD_DEVINFO.hashCode()) {
                showToast("获取设备信息成功！");
                mK2_devInfoStruct = (K2_DevInfoStruct) msg.obj;
                mTvCustomerId.setText(mK2_devInfoStruct.getCustomer_id());
                mTvHardwareId.setText(mK2_devInfoStruct.getHardwareVer());
                mTvCodeId.setText(mK2_devInfoStruct.getSoftwareVer());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_dev_info);
        setTitle("获取设备信息");
        mK2Proxy = BleFactory.getInstance().getK2Proxy();
        mK2Proxy.setHandler(mK2Handler);
        mK2_devInfoStruct = new K2_DevInfoStruct();
        mTvCodeId = (TextView) findViewById(R.id.tv_software_id);
        mTvHardwareId = (TextView) findViewById(R.id.tv_hardware_id);
        mTvCustomerId = (TextView) findViewById(R.id.tv_customer_id);
        findViewById(R.id.btn_get_dev_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mK2Proxy.isConnectOK()) {
                    showToast("蓝牙已经断开");
                    return;
                }
                mK2Proxy.getSendHelper().getDevInfo();
            }
        });

    }

    private void showToast(String input) {
        Toast.makeText(this, input, Toast.LENGTH_SHORT).show();
    }
}
